package com.sinodata.bsm.cicp.util.telnet;

import java.io.IOException;
import org.apache.commons.lang.StringUtils;

/**
 * Telnet 任务.
 * 
 * @author thomas.h.zhang
 */
public class CheckThread extends Thread {
    public CheckThread() {
    }

    public CheckThread(Telnet telnet, String cmd) {
        this.setTelnet(telnet);
        this.setCmd(cmd);
    }

    public void run() {
        while (true) {
            try {
                shellOut = telnet.recvResp();
                if (StringUtils.startsWith(shellOut, cmd)) {
                    this.setComplete(true);
                    break;
                }

            } catch (IOException e) {
                this.setComplete(false);
                this.setError(true);
                this.setErrorMsg(e.getMessage());
            }
        }
        this.setActive(false);
        return;
    }

    protected boolean active = true;
    protected boolean complete = false;
    protected boolean error = false;
    protected String errorMsg;
    protected String cmd;
    protected String shellOut; // Shell执行结果.
    protected Telnet telnet;

    public Telnet getTelnet() {
        return telnet;
    }

    public void setTelnet(Telnet telnet) {
        this.telnet = telnet;
    }

    public String getCmd() {
        return cmd;
    }

    public void setCmd(String cmd) {
        this.cmd = cmd;
    }

    public String getShellOut() {
        return shellOut;
    }

    public void setShellOut(String shellOut) {
        this.shellOut = shellOut;
    }

    /**
     * Checks if this TelnetTask is complete.
     */
    public boolean isComplete() {
        return complete;
    }

    public void setComplete(boolean complete) {
        this.complete = complete;
    }

    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }

    public boolean isError() {
        return error;
    }

    public void setError(boolean error) {
        this.error = error;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

}
